package servlet;

import Hbase.AllStudent;
import pojo.ST;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@WebServlet("/AllServlet")
public class AllServlet extends HttpServlet {
    public void init(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
     req.setCharacterEncoding("utf-8");
     resp.setContentType("text/html;charset=utf-8");
     resp.setCharacterEncoding("utf-8");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        init(req,resp);
        AllStudent all=new AllStudent();
        List<ST> display = all.main();

        Gson gson = new Gson();
        //发送JSON 数据给前台
        String listJson = gson.toJson(display);
        // list 转为json  字符串
        // 返回客户端的是字符串
        // 客户端再转为json 对象

        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(listJson);



//api 可用 前端进行 渲染   http://localhost:8080/AllServlet



//        resp.setContentType("text/html;charset=utf-8");
//        PrintWriter pw = resp.getWriter();
//        for (int i = 0; i < display.size(); i++) {
////ST　　学生类
//            System.out.println("list 中的元素----》"+display.get(i));
//
//            System.out.println( display.get(i).getStudentID());
//            System.out.println( display.get(i).getStudentDate());
//            pw.write(display.get(i).getStudentID()+"   ");
//            pw.write(display.get(i).getStudentName()+"   ");
//            pw.write(display.get(i).getStudentSemester()+"   ");
//            pw.write(display.get(i).getStudentNumber()+"   ");
//            pw.write(display.get(i).getStudentDate()+"   ");
//            pw.write(display.get(i).getStudentStatus()+"<br>");
//
//        }
    }
}
